import { vec3 } from "wgpu-matrix"

type v3 = [number, number, number]
type v4 = [number, number, number, number]
type v2 = [number, number]


export class Tessellator {
    vectices: number[] = []
    uvs: number[] = []
    normals: number[] = []

    protected uvDown: v2 = [0, 1]
    protected uvRight: v2 = [1, 0]
    protected uvOrigin: v2 = [0, 0]

    addFace(origin: v3, right: v3, up: v3) {
        const uvOrigin = this.uvOrigin
        const uvDown = this.uvDown
        const uvRight = this.uvRight

        const v3r = vec3.create(...right)
        const v3u = vec3.create(...up)
        const normal = vec3.cross(v3r, v3u)

        this.vectices.push(...[
            origin[0],              origin[1],              origin[2], 1,
            origin[0] + up[0],      origin[1] + up[1],      origin[2] + up[2], 1,
            origin[0] + right[0],   origin[1] + right[1],   origin[2] + right[2], 1,
        
            origin[0] + up[0],              origin[1] + up[1],              origin[2] + up[2], 1,
            origin[0] + right[0] + up[0],   origin[1] + right[1] + up[1],   origin[2] + right[2] + up[2], 1,
            origin[0] + right[0],           origin[1] + right[1],           origin[2] + right[2], 1,
        ])

        this.uvs.push(...[
            uvOrigin[0] + uvDown[0],                uvOrigin[1] + uvDown[1], 
            uvOrigin[0],                            uvOrigin[1],
            uvOrigin[0] + uvRight[0] + uvDown[0],   uvOrigin[1] + uvRight[1] + uvDown[1],
            
            uvOrigin[0],                                uvOrigin[1],
            uvOrigin[0] + uvRight[0],                   uvOrigin[1] + uvRight[1],
            uvOrigin[0] + uvRight[0] + uvDown[0],       uvOrigin[1] + uvRight[1] + uvDown[1],
        ])

        this.normals.push(...[
            normal[0], normal[1], normal[2], 0,
            normal[0], normal[1], normal[2], 0,
            normal[0], normal[1], normal[2], 0,
            normal[0], normal[1], normal[2], 0,
            normal[0], normal[1], normal[2], 0,
            normal[0], normal[1], normal[2], 0,
        ])
    }
}
